<!--$Id: join.so,v 10.31 2004/01/21 20:39:56 bostic Exp $-->
<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Equality Join</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<a name="2"><!--meow--></a><a name="3"><!--meow--></a><a name="4"><!--meow--></a>
<table width="100%"><tr valign=top>
<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></b></td>
<td align=right><a href="../am/curdup.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am/count.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p align=center><b>Equality Join</b></p>
<p>Berkeley DB supports "equality" (also known as "natural"), joins on secondary
indices.  An equality join is a method of retrieving data from a primary
database using criteria stored in a set of secondary indices.  It
requires the data be organized as a primary database which contains the
primary key and primary data field, and a set of secondary indices.
Each of the secondary indices is indexed by a different secondary key,
and, for each key in a secondary index, there is a set of duplicate data
items that match the primary keys in the primary database.</p>
<p>For example, let's assume the need for an application that will return
the names of stores in which one can buy fruit of a given color.  We
would first construct a primary database that lists types of fruit as
the key item, and the store where you can buy them as the data item:</p>
<blockquote><table border=1>
<tr><th>Primary key:</th><th>Primary data:</th></tr>
<tr>    <td align=left>apple</td>    <td align=left>Convenience Store</td>    </tr>
<tr>    <td align=left>blueberry</td>    <td align=left>Farmer's Market</td>    </tr>
<tr>    <td align=left>peach</td>    <td align=left>Shopway</td>    </tr>
<tr>    <td align=left>pear</td>    <td align=left>Farmer's Market</td>    </tr>
<tr>    <td align=left>raspberry</td>    <td align=left>Shopway</td>    </tr>
<tr>    <td align=left>strawberry</td>    <td align=left>Farmer's Market</td>    </tr>
</table></blockquote>
<p>We would then create a secondary index with the key <b>color</b>, and,
as the data items, the names of fruits of different colors.</p>
<blockquote><table border=1>
<tr><th>Secondary key:</th><th>Secondary data:</th></tr>
<tr>    <td align=left>blue</td>    <td align=left>blueberry</td>    </tr>
<tr>    <td align=left>red</td>    <td align=left>apple</td>    </tr>
<tr>    <td align=left>red</td>    <td align=left>raspberry</td>    </tr>
<tr>    <td align=left>red</td>    <td align=left>strawberry</td>    </tr>
<tr>    <td align=left>yellow</td>    <td align=left>peach</td>    </tr>
<tr>    <td align=left>yellow</td>    <td align=left>pear</td>    </tr>
</table></blockquote>
<p>This secondary index would allow an application to look up a color, and
then use the data items to look up the stores where the colored fruit
could be purchased.  For example, by first looking up <b>blue</b>,
the data item <b>blueberry</b> could be used as the lookup key in the
primary database, returning <b>Farmer's Market</b>.</p>
<p>Your data must be organized in the following manner in order to use the
<a href="../../api_c/db_join.html">DB-&gt;join</a> method:</p>
<ol>
<p><li>The actual data should be stored in the database represented by the
<a href="../../api_c/db_class.html">DB</a> object used to invoke this method.  Generally, this
<a href="../../api_c/db_class.html">DB</a> object is called the <i>primary</i>.
<p><li>Secondary indices should be stored in separate databases, whose keys
are the values of the secondary indices and whose data items are the
primary keys corresponding to the records having the designated
secondary key value.  It is acceptable (and expected) that there may be
duplicate entries in the secondary indices.
<p>These duplicate entries should be sorted for performance reasons, although
it is not required.  For more information see the <a href="../../api_c/db_set_flags.html#DB_DUPSORT">DB_DUPSORT</a> flag
to the <a href="../../api_c/db_set_flags.html">DB-&gt;set_flags</a> method.</p>
</ol>
<p>What the <a href="../../api_c/db_join.html">DB-&gt;join</a> method does is review a list of secondary keys, and,
when it finds a data item that appears as a data item for all of the
secondary keys, it uses that data item as a lookup into the primary
database, and returns the associated data item.</p>
<p>If there were another secondary index that had as its key the <b>cost</b>
of the fruit, a similar lookup could be done on stores where inexpensive
fruit could be purchased:</p>
<blockquote><table border=1>
<tr><th>Secondary key:</th><th>Secondary data:</th></tr>
<tr>    <td align=left>expensive</td>    <td align=left>blueberry</td>    </tr>
<tr>    <td align=left>expensive</td>    <td align=left>peach</td>    </tr>
<tr>    <td align=left>expensive</td>    <td align=left>pear</td>    </tr>
<tr>    <td align=left>expensive</td>    <td align=left>strawberry</td>    </tr>
<tr>    <td align=left>inexpensive</td>    <td align=left>apple</td>    </tr>
<tr>    <td align=left>inexpensive</td>    <td align=left>pear</td>    </tr>
<tr>    <td align=left>inexpensive</td>    <td align=left>raspberry</td>    </tr>
</table></blockquote>
<p>The <a href="../../api_c/db_join.html">DB-&gt;join</a> method provides equality join functionality.  While not
strictly cursor functionality, in that it is not a method off a cursor
handle, it is more closely related to the cursor operations than to the
standard <a href="../../api_c/db_class.html">DB</a> operations.</p>
<p>It is also possible to do lookups based on multiple criteria in a single
operation.  For example, it is possible to look up fruits that are both
red and expensive in a single operation.  If the same fruit appeared as
a data item in both the color and expense indices, then that fruit name
would be used as the key for retrieval from the primary index, and would
then return the store where expensive, red fruit could be purchased.</p>
<b>Example</b>
<p>Consider the following three databases:</p>
<br>
<b>personnel</b><ul compact><li><p><ul type=disc>
<li>key = SSN
<li>data = record containing name, address, phone number, job title
</ul></ul>
<b>lastname</b><ul compact><li><p><ul type=disc>
<li>key = lastname
<li>data = SSN
</ul></ul>
<b>jobs</b><ul compact><li><p><ul type=disc>
<li>key = job title
<li>data = SSN
</ul></ul>
<br>
<p>Consider the following query:</p>
<blockquote><pre>Return the personnel records of all people named smith with the job
title manager.</pre></blockquote>
<p>This query finds are all the records in the primary database (personnel)
for whom the criteria <b>lastname=smith and job title=manager</b> is
true.</p>
<p>Assume that all databases have been properly opened and have the
handles:  pers_db, name_db, job_db.  We also assume that we have an
active transaction to which the handle txn refers.</p>
<blockquote><pre>DBC *name_curs, *job_curs, *join_curs;
DBC *carray[3];
DBT key, data;
int ret, tret;
<p>
name_curs = NULL;
job_curs = NULL;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
<p>
if ((ret =
    name_db-&gt;cursor(name_db, txn, &name_curs, 0)) != 0)
	goto err;
key.data = "smith";
key.size = sizeof("smith");
if ((ret =
    name_curs-&gt;c_get(name_curs, &key, &data, DB_SET)) != 0)
	goto err;
<p>
if ((ret = job_db-&gt;cursor(job_db, txn, &job_curs, 0)) != 0)
	goto err;
key.data = "manager";
key.size = sizeof("manager");
if ((ret =
    job_curs-&gt;c_get(job_curs, &key, &data, DB_SET)) != 0)
	goto err;
<p>
carray[0] = name_curs;
carray[1] = job_curs;
carray[2] = NULL;
<p>
if ((ret =
    pers_db-&gt;join(pers_db, carray, &join_curs, 0)) != 0)
	goto err;
while ((ret =
    join_curs-&gt;c_get(join_curs, &key, &data, 0)) == 0) {
	/* Process record returned in key/data. */
}
<p>
/*
 * If we exited the loop because we ran out of records,
 * then it has completed successfully.
 */
if (ret == DB_NOTFOUND)
	ret = 0;
<p>
err:
if (join_curs != NULL &&
    (tret = join_curs-&gt;c_close(join_curs)) != 0 && ret == 0)
	ret = tret;
if (name_curs != NULL &&
    (tret = name_curs-&gt;c_close(name_curs)) != 0 && ret == 0)
	ret = tret;
if (job_curs != NULL &&
    (tret = job_curs-&gt;c_close(job_curs)) != 0 && ret == 0)
	ret = tret;
<p>
return (ret);
</pre></blockquote>
<p>The name cursor is positioned at the beginning of the duplicate list
for <b>smith</b> and the job cursor is placed at the beginning of
the duplicate list for <b>manager</b>.  The join cursor is returned
from the join method.  This code then loops over the join cursor getting
the personnel records of each one until there are no more.</p>
<table width="100%"><tr><td><br></td><td align=right><a href="../am/curdup.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am/count.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
</body>
</html>
